load("//bazel:yr.bzl", "cc_strip")
load("//bazel:yr_go.bzl", "yr_go_test")

filegroup(
    name = "go_sources",
    srcs = glob([
        "**/*.go",
        "**/go.mod",
    ]),
)

cc_strip(
    name = "go_strip",
    srcs = ["//api/go/libruntime/cpplibruntime:libcpplibruntime.so"],
)

genrule(
    name = "yr_go_pkg",
    srcs = [
        "go_strip",
        "//:grpc_strip",
        "@datasystem_sdk//:shared",
        "@metrics_sdk//:shared",
        ":go_sources",
    ],
    outs = ["yr_go_pkg.out"],
    cmd = """
        BASE_DIR="$$(pwd)" &&
        GO_OUT_DIR=$$BASE_DIR/build/output/runtime/service/go/bin/ &&
        DATASYSTEM_FILE=$$(echo $(locations @datasystem_sdk//:shared) | awk '{print $$1}') &&
        DATASYSTEM_DIR=$$(dirname $$BASE_DIR/$$DATASYSTEM_FILE) &&
        METRICS_FILE=$$(echo $(locations @metrics_sdk//:shared) | awk '{print $$1}') &&
        METRICS_DIR=$$(dirname $$BASE_DIR/$$METRICS_FILE) &&
        mkdir -p $$GO_OUT_DIR &&
        cd $$BASE_DIR &&
        chmod +w $(locations :go_strip) $(locations //:grpc_strip) &&
        chrpath -d $(locations :go_strip) $(locations //:grpc_strip) &&
        cp -ar $(locations :go_strip) $(locations //:grpc_strip) $$GO_OUT_DIR &&
        cp -ar $$DATASYSTEM_DIR/* $$GO_OUT_DIR &&
        cp -ar $$METRICS_DIR/* $$GO_OUT_DIR &&
        cd $$(realpath $$BASE_DIR/api/go/runtime/) &&
        go mod tidy &&
        LD_LIBRARY_PATH=$${LD_LIBRARY_PATH:+$$LD_LIBRARY_PATH}:$$GO_OUT_DIR CC='gcc -fstack-protector-strong -D_FORTIFY_SOURCE=2 -O2' go build -buildmode=pie -ldflags '-extldflags "-fPIC -fstack-protector-strong -Wl,-z,now,-z,relro,-z,noexecstack,-s -Wall -Werror"' -o $$GO_OUT_DIR/goruntime yr_runtime_main.go &&
        cd $$BASE_DIR &&
        chmod -R 750 $$GO_OUT_DIR &&
        GO_SDK_DIR=$$BASE_DIR/build/output/runtime/sdk/go/runtime &&
        rm -rf $$GO_SDK_DIR &&
        mkdir -p $$GO_SDK_DIR &&
        cp -arf $$BASE_DIR/api/go/libruntime $$GO_SDK_DIR &&
        find $$GO_SDK_DIR/libruntime/ -name *.cpp | xargs rm -rf &&
        find $$GO_SDK_DIR/libruntime/ -name *.bazel | xargs rm -rf &&
        cp -arf $$BASE_DIR/api/go/faassdk $$GO_SDK_DIR &&
        cp -arf $$BASE_DIR/api/go/posixsdk $$GO_SDK_DIR &&
        cp -arf $$BASE_DIR/api/go/yr $$GO_SDK_DIR &&
        cp -arf $$BASE_DIR/api/go/go.mod $$GO_SDK_DIR &&
        cp -arf $$BASE_DIR/api/go/README.md $$GO_SDK_DIR &&
        echo "$$GO_OUT_DIR" > $@
    """,
    local = True,
    visibility = ["//visibility:public"],
)

config_setting(
    name = "asan",
    values = {"define": "sanitize=address"},
)

config_setting(
    name = "tsan",
    values = {"define": "sanitize=thread"},
)

alias(
    name = "yr_go_test",
    actual = select({
        ":asan": "yr_go_test_asan",
        ":tsan": "yr_go_test_tsan",
        "//conditions:default": "yr_go_test_default",
    }),
)

yr_go_test(
    name = "yr_go_test_asan",
    sanitizer = "address",
)

yr_go_test(
    name = "yr_go_test_tsan",
    sanitizer = "thread",
)

yr_go_test(
    name = "yr_go_test_default",
    sanitizer = "off",
)
